#include "glutMouseManager.h"
#include <iostream>
#include <GL\glut.h>


int glutMouseManager::_oldX = 0;
int glutMouseManager::_oldY = 0;
int glutMouseManager::_currentX = 0;
int glutMouseManager::_currentY = 0;
int* glutMouseManager::_state = new int[MOUSE_SIZE];

void glutMouseManager::Update()
{
	_oldX = _currentX;
	_oldY = _currentY;
	for(int  i = 0; i < MOUSE_SIZE; i++)
	{
		if(_state[i] == FIRST_PRESS)
		{
			_state[i] =	HELD_DOWN;
		}
	}
}


void glutMouseManager::ButtonPresed(int button, int state, int x, int y)
{
	//std::cout << "Button: " << button << "  State: " << state << std::endl;
	if(state) // state 1 means release 1 = true
	{
		_state[button] = NOT_PRESSED;
	}
	else // 0 means it has been presed
	{
		_state[button] = FIRST_PRESS;
	}
	_currentX = x;
	_currentY = y;
}

void glutMouseManager::MouseMoved(int x, int y)
{
	_currentX = x;
	_currentY = y;
}

void glutMouseManager::Begin()
{
	glutMouseFunc(ButtonPresed);
	glutMotionFunc(MouseMoved);
	glutPassiveMotionFunc(MouseMoved);
}


bool glutMouseManager::ButtonPresed(int button)	
{
	if(_state[button] == FIRST_PRESS || _state[button] == HELD_DOWN)
	{
		return true;
	}
	return false;
}
bool glutMouseManager::SinglePress(int button)
{
	return _state[button] == FIRST_PRESS;
}
	

int  glutMouseManager::X()
{
	return _currentX;
}
int  glutMouseManager::Y()
{
	return _currentY;
}
int  glutMouseManager::XChange()
{
	return _currentX - _oldX;
}
int  glutMouseManager::YChange()
{
	return _currentY - _oldY;
}